package com.hlk.singleton;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-11-24 10:09
 */
public class LazyDoubleCheckSingleton {
    // volatile：防止重排序
    private volatile static LazyDoubleCheckSingleton lazyDoubleCheckSingleton = null;

    private LazyDoubleCheckSingleton() {
    }

    public synchronized static LazyDoubleCheckSingleton getInstance() {
        if (lazyDoubleCheckSingleton == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (lazyDoubleCheckSingleton == null) {
                    // 注意指令重排出现的问题
                    lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
                    // 1.分配内存给这个对象
                    // 2.设置lazyDoubleCheckSingleton指向刚分配的内存地址
                    // 3.初始化对象
                }
            }
        }
        return lazyDoubleCheckSingleton;
    }
}
